﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace Application.Admin.ExportImport
{
    /// <summary>
    /// Export manager
    /// </summary>
    public partial class ExportService : IExportService
    {
        public ExportService()
        {
        }

        #region Methods

        /// <summary>
        /// Exports the sight to XLS.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="sight">The sight.</param>
        public virtual void ExportSightToXls(string filePath, IList<Domain.Entity.SightPoint> sight)
        {
            using (var excelHelper = new Infrastructure.Crosscutting.Comm.Service.ExcelService(filePath))
            {
                excelHelper.Hdr = "YES";
                excelHelper.Imex = "0";
                var tableDefinition = new Dictionary<string, string>();
                //standard properties
                tableDefinition.Add("CustomerId", "int");
                tableDefinition.Add("Fax", "nvarchar(255)");

                tableDefinition.Add("AvatarPictureId", "int");
                tableDefinition.Add("ForumPostCount", "int");
                tableDefinition.Add("Signature", "nvarchar(255)");
                excelHelper.WriteTable("Customers", tableDefinition);

                //string decimalQuoter = (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.Equals(",") ? "\"" : String.Empty);

                foreach (var item in sight)
                {
                    var sb = new StringBuilder();
                    sb.Append("INSERT INTO [Customers] (CustomerId,CustomerGuid,Email,Username,PasswordStr,PasswordFormatId,PasswordSalt,LanguageId,CurrencyId,TaxDisplayTypeId,IsTaxExempt,VatNumber,VatNumberStatusId,TimeZoneId,AffiliateId,Active,Deleted,IsGuest,IsRegistered,IsAdministrator,IsForumModerator,FirstName,LastName,Gender,Company,StreetAddress,StreetAddress2,ZipPostalCode,City,CountryId,StateProvinceId,Phone,Fax,AvatarPictureId,ForumPostCount,Signature) VALUES (");

                    sb.Append(item.Id); sb.Append(",");
                    //sb.Append('"'); sb.Append(item.CustomerGuid); sb.Append("\",");
                    //sb.Append('"'); sb.Append(item.Email != null ? item.Email.Replace('"', '\'') : ""); sb.Append("\",");
                    //sb.Append('"'); sb.Append(item.Username != null ? item.Username.Replace('"', '\'') : ""); sb.Append("\",");
                    //sb.Append('"'); sb.Append(item.Password != null ? item.Password.Replace('"', '\'') : ""); sb.Append("\",");
                    //sb.Append(item.PasswordFormatId); sb.Append(",");

                    excelHelper.ExecuteCommand(sb.ToString());
                }
            }
        }

        #endregion
    }
}
